using System.Collections.Generic;
using TestCases.CompareAssert.Core.Compositions.Interfaces;
using TestCases.CompareAssert.Core.Interfaces;

namespace TestCases.CompareAssert.Core.Compositions
{
    public class AllStrategy<TItem, TResult>: ICompositionStrategy<TItem, TResult> where TResult: IResultProvider
    {
        
        public IResultBuilder Execute(ICompositionComparer<TItem, TResult> comparer, IEnumerable<TItem> items, ICompareContext context)
        {
            bool isNotApplicable = true;
            var resultItems = new List<TResult>();
            
            foreach (var item in items)
            {               
                var resultItem = comparer.CompareBy(item, context);
                resultItems.Add(resultItem);
                
                switch (resultItem.GetStatus())
                {
                    case CompareStatus.NotEquals:
                        return comparer.ComposeItemResults(CompareStatus.NotEquals, context, resultItems);

                    case CompareStatus.Equals:
                        isNotApplicable = false;
                        break;
                }
            }
            return (isNotApplicable) 
                ? comparer.ComposeItemResults(CompareStatus.NotApplicable, context, resultItems)
                : comparer.ComposeItemResults(CompareStatus.Equals, context, resultItems);
        }
    }
}